package com.evan.review.eventdispatch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
 * 自定义FrameLayout，用于演示事件分发机制
 * 通过重写事件分发相关方法，展示事件流向
 */
public class MyFrameLayout extends FrameLayout {
    
    private boolean consumeEvent = false;
    
    public MyFrameLayout(Context context) {
        super(context);
    }
    
    public MyFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    /**
     * 设置是否消费事件
     */
    public void setConsumeEvent(boolean consumeEvent) {
        this.consumeEvent = consumeEvent;
    }
    
    /**
     * 事件分发方法
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        EventExampleActivity activity = EventExampleActivity.getInstance(this);
        if (activity != null) {
            activity.appendLog("MyFrameLayout - dispatchTouchEvent: " + getEventName(ev));
        }
        
        boolean result = super.dispatchTouchEvent(ev);
        
        if (activity != null) {
            activity.appendLog("MyFrameLayout - dispatchTouchEvent 返回: " + result);
        }
        
        return result;
    }
    
    /**
     * 事件拦截方法
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        EventExampleActivity activity = EventExampleActivity.getInstance(this);
        
        // 根据当前设置决定是否拦截事件
        boolean shouldIntercept = consumeEvent;
        
        if (activity != null) {
            activity.appendLog("MyFrameLayout - onInterceptTouchEvent: " + getEventName(ev) + 
                    (shouldIntercept ? " (拦截)" : " (不拦截)"));
            
            // 当拦截DOWN事件时显示Toast提示
            if (ev.getAction() == MotionEvent.ACTION_DOWN && shouldIntercept) {
                activity.showEventConsumedToast("布局拦截");
            }
        }
        
        return shouldIntercept;
    }
    
    /**
     * 事件处理方法
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        EventExampleActivity activity = EventExampleActivity.getInstance(this);
        
        if (activity != null) {
            activity.appendLog("MyFrameLayout - onTouchEvent: " + getEventName(event) + 
                    (consumeEvent ? " (消费)" : " (不消费)"));
        }
        
        // 当ACTION_UP事件发生且我们消费事件时，显示Toast
        if (event.getAction() == MotionEvent.ACTION_UP && consumeEvent && activity != null) {
            activity.showEventConsumedToast("布局onTouchEvent消费");
        }
        
        return consumeEvent;
    }
    
    /**
     * 获取事件名称
     */
    private String getEventName(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return "ACTION_DOWN";
            case MotionEvent.ACTION_MOVE:
                return "ACTION_MOVE";
            case MotionEvent.ACTION_UP:
                return "ACTION_UP";
            case MotionEvent.ACTION_CANCEL:
                return "ACTION_CANCEL";
            default:
                return "OTHER";
        }
    }
} 